<template>
  <PanelHeader :route="route"></PanelHeader>
  <div class="btns">
    <el-button @click="open(null)" type="primary" icon="Plus" size="small">新增</el-button>
    <el-popconfirm confirm-button-text="是" cancel-button-text="否" :icon="InfoFilled" icon-color="#626AEF" title="是否确认删除"
      @confirm="confirmEvent" @cancel="cancelEvent">
      <template #reference>
        <el-button type="danger" size="small" icon="Delete">删除</el-button>
      </template>
    </el-popconfirm>
  </div>
  <el-table :data="tableData.list" style="width: 100%;" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55"></el-table-column>
    <el-table-column prop="id" label="id"> </el-table-column>
    <el-table-column prop="name" label="昵称"> </el-table-column>
    <el-table-column label="头像">
      <template #default="scope">
        <el-image style="width: 50pdx;height: 50px;" :src="scope.row.avatar"></el-image>
      </template>
    </el-table-column>
    <el-table-column prop="sex" label="性别">
      <template #default="scope">
        {{ scope.row.sex === '1' ? '男' : "女" }}
      </template>
    </el-table-column>
    <el-table-column prop="mobile" label="手机号"> </el-table-column>
    <el-table-column prop="active" label="状态">
      <template #default="scope">
        <el-tag :type="scope.row.active ? 'success' : 'danger'">{{ scope.row.active ? '正常' : '失效' }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="创建时间">
      <template #default="scope">
        <div class="flex-box">
          <el-icon>
            <Clock />
          </el-icon>
          <span>{{ scope.row.create_time }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <div class="pagination-info">
    <el-pagination v-model:current-page="pagiantionsData.pageNum" :page-size="pagiantionsData.pageSize"
      :background="background" layout="total, prev, pager, next" :total="tableData.total" size="small"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
  <el-dialog v-model="dialogFormVisible" :before-close="beforeClose" title="陪护师添加" width="500">
    <el-form ref="formRef" label-width="100px" label-position="left" :model="form" :rules="rules">
      <el-form-item v-show="false" prop="id">
        <el-input v-model="form.id"></el-input>
      </el-form-item>
      <el-form-item prop="name" label="昵称">
        <el-input v-model="form.name" placeholder="请输入昵称"></el-input>
      </el-form-item>
      <el-form-item prop="avatar" label="头像">
        <el-button type="primary" v-if="!form.avatar" @click="dialogImageVisible = true">点击上传</el-button>
        <el-image v-else :src="form.avatar" style="width: 100px;height: 100px;"></el-image>
      </el-form-item>
      <el-form-item prop="sex" label="性别">
        <el-select v-model="form.sex" placeholder="请选择性别">
          <el-option label="男" value="1"></el-option>
          <el-option label="女" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="age" label="年龄">
        <el-input-number :min="18" :max="65" v-model="form.age"> </el-input-number>
      </el-form-item>
      <el-form-item prop="mobile" label="手机号">
        <el-input v-model="form.mobile" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item prop="active" label="是否生效">
        <!-- 单选 -->
        <el-form-item label="" prop="">
          <el-radio-group v-model="form.active">
            <el-radio :value="0">失效</el-radio>
            <el-radio :value="1">生效</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dislog-footer">
        <el-button type="primary" @click="confirm(formRef)">确认</el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog v-model="dialogImageVisible" :before-close="beforeClose" title="选择图片" width="680">
    <div class="image-list">
      <div v-for="(item, index) in avatarList" :key="item.name" class="img-box" @click="selectIndex = index">
        <div v-if="selectIndex === index" class="select">
          <el-icon color="#fff">
            <Check />
          </el-icon>
        </div>
        <el-image :src="item.url" style="width: 148px;height: 148px;"></el-image>
      </div>
    </div>
    <template #footer>
      <div class="dislog-footer">
        <el-button @click="dialogImageVisible = false" type="default">取消</el-button>
        <el-button type="primary" @click="confirmImage()">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, onMounted, nextTick } from 'vue';
import { photoList, companion, companionList, deleteCompanion } from '../../../api/index'
import { ElMessage } from 'element-plus';
import { useRoute } from 'vue-router';
const route = useRoute()
const formRef = ref()
const tableData = reactive({
  list: [],
  total: 0
})
const pagiantionsData = reactive({
  pageNum: 1,
  pageSize: 10
})
const dialogFormVisible = ref(false)
//头像弹窗
const dialogImageVisible = ref(false)
const avatarList = ref([])
const selectIndex = ref(0)
const selectTableData = ref([])
onMounted(() => {
  photoList().then(({ data }) => {
    avatarList.value = data.data
  })
  getListData()
})
const getListData = () => {
  companionList(pagiantionsData).then(({ data }) => {
    const { total, list } = data.data
    tableData.list = list
    tableData.total = total
  })
}
const beforeClose = () => {
  dialogFormVisible.value = false
  Object.assign(form, {
    id: '',
    mobile: '',
    active: 1,
    age: 28,
    avatar: '',
    name: '',
    sex: ''
  })
}

const form = reactive({
  id: '',
  mobile: '',
  active: 1,
  age: 28,
  avatar: '',
  name: '',
  sex: ''
})
const rules = reactive({
  name: [{ required: true, trigger: 'blur', message: '请填写昵称' }],
  avatar: [{ required: true, message: '请选择头像' }],
  sex: [{ required: true, trigger: 'blur', message: '请选择性别' }],
  mobile: [{ required: true, trigger: 'blur', message: '请填写手机号' }]
})
const confirm = async (formRef) => {
  if (!formRef) return
  await formRef.validate((valid, fields) => {
    if (valid) {
      companion(form).then(({ data }) => {
        if (data.code === 10000) {
          ElMessage.success('成功')
          beforeClose()
          getListData()
        } else {
          ElMessage.error(data.message)
        }
      })
    }
    else {
      console.log("error");
    }
  })
}
const open = (rowData = {}) => {
  dialogFormVisible.value = true
  nextTick(() => {
    // /如果是编辑
    if (rowData) {
      Object.assign(form, rowData)
    }
  })
}
const confirmImage = () => {
  form.avatar = avatarList.value[selectIndex.value].url
  dialogImageVisible.value = false
}
const handleSizeChange = (val) => {
  pagiantionData.pageSize = val
  getList()
}
const handleCurrentChange = (val) => {
  pagiantionData.pageNum = val
  getList()
}
const handleSelectionChange = (val) => {
  selectTableData.value = val.map(item => ({ id: item.id }))
}
const confirmEvent = () => {
  // /没有选择要删除的选项
  if (!selectTableData.value.length) {
    return ElMessage.warning('至少选择一项数据')
  }
  deleteCompanion({ id: selectTableData.value }).then(({ data }) => {
    if (data.code === 10000) {
      getListData()
    }
  })
}
</script>

<style lang="less" scoped>
.btns {
  padding: 10px 0 10px 10px;
  background-color: #fff;
}

.image-list {
  display: flex;
  align-items: center;
  flex-wrap: wrap;

  .img-box {
    position: relative;

    .select {
      position: absolute;
      left: 0px;
      top: 0px;
      width: 24px;
      height: 24px;
      background-color: #67c23a;
      z-index: 999;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }

  .el-image {
    margin-right: 10px;
    margin-bottom: 10px;
  }
}
</style>
